from apps.system.models import Permission
from extensions.serializers import *


class GetTokenRequest(Serializer):
    number = CharField(label='Team 编号')
    username = CharField(label='用户名')
    password = CharField(label='密码')

class SMSRequest(Serializer):
    mobile = CharField(label='手机号码')
    sys_name = CharField(label='系统名称')

class SMSGetTokenRequest(Serializer):
    mobile = CharField(label='手机号码')
    code = CharField(label='验证码')
    sys_name = CharField(label='系统名称')


class GetTokenResponse(Serializer):
    refresh = CharField(label='刷新令牌')
    access = CharField(label='访问令牌')


class RefreshTokenRequest(Serializer):
    refresh = CharField(label='刷新令牌')


class RefreshTokenResponse(Serializer):
    access = CharField(label='访问令牌')


class PermissionSerializer(ModelSerializer):
    class Meta:
        model = Permission
        fields = '__all__'  # 或者列出你想要的字段

class UserInfoResponse(Serializer):

    id = IntegerField(label='用户ID')
    name = CharField(label='名字')
    phone = CharField(label='手机号码')
    is_superuser = BooleanField(label='管理员状态')
    permissions = SerializerMethodField()  # 添加这一行

    def get_permissions(self, obj):
        """获取和序列化权限数据"""
        permissions = obj.permissions.all()
        # return PermissionSerializer(permissions, many=True).data
        return [permission.code for permission in permissions]

class SetPasswordRequest(Serializer):
    old_password = CharField(label='旧密码')
    new_password = CharField(label='新密码')


__all__ = [
    'GetTokenRequest', 'GetTokenResponse',
    'RefreshTokenRequest', 'RefreshTokenResponse',
    'UserInfoResponse', 'SetPasswordRequest',
]
